// 用户任务节点
import {CircleNode, CircleNodeModel, GraphModel, LogicFlow} from '@logicflow/core'
import {genBpmnId} from "@logicflow/extension/src/bpmn-elements/utils";
import {FlowNodeTypeEnum} from "@/enums/FlowNodeTypeEnum"
import {sourceRulesOnlyOneOut, targetRulesNotIn} from "./rules/NodeValidateRules"

// 自定义业务节点外观
class view extends CircleNode {
}

class model extends CircleNodeModel {

    // 节点初始化
    constructor(data: NodeConfig, graphModel: GraphModel) {
        if (!data.id) {
            data.id = `start_${genBpmnId()}`
        }
        super(data, graphModel)
    }

    // 自定义节点形状（圆角矩形）
    initNodeData(data: LogicFlow.NodeConfig) {
        super.initNodeData(data);
        // 调整圆的半径
        this.r = 30
        // 不允许文本被编辑
        this.text.editable = false
    }

    // 自定义业务节点样式
    getNodeStyle() {
        const style = super.getNodeStyle()
        return style
    }

    // 获取当前节点作为边的起始节点规则
    getConnectedSourceRules(): Model.ConnectRule[] {
        const rules = super.getConnectedSourceRules();
        rules.push(sourceRulesOnlyOneOut);
        return rules;
    }
    // 获取当前节点作为边的目标节点规则。
    getConnectedTargetRules(){
        const rules = super.getConnectedTargetRules();
        rules.push(targetRulesNotIn)
        return rules;
    }
}

// 注册组件
export function register(lf: LogicFlow) {
    lf.register({
        type: FlowNodeTypeEnum.FLOW_START,
        view: view,
        model: model,
    })
}

